package com.iot.ammeter.controller.campus;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.campus.RoomDTO;
import com.iot.ammeter.query.web.campus.RoomQuery;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.campus.RoomService;
import com.iot.ammeter.utils.EasyExcelExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房间表 前端控制器
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@RestController
@Api(tags = {"房间管理"})
@RequestMapping("/api/room")
public class RoomController {
    private final RoomService roomService;

    public RoomController(RoomService roomService) {
        this.roomService = roomService;
    }

    /**
     * 获取房间信息列表
     *
     * @param page      请求页码
     * @param pageSize  每页数量
     * @param roomQuery 房间查询条件
     * @return 返回取房间信息列表json数据
     */
    @ApiOperation("获取房间信息列表")
    @LogAnnotation(module = "getRoomList", operator = "获取房间信息列表")
    @PostMapping("/list/{page}/{pageSize}")
    public ResponseResult getRoomList(@PathVariable int page, @PathVariable int pageSize, @RequestBody(required = false) RoomQuery roomQuery) {
        /**
         * 数据校验
         */
        if (page <= 0 || pageSize <= 0) {
            return ResponseResult.FAILED("页码参数错误");
        }
        IPage<RoomDTO> roomList = roomService.getRoomList(page, pageSize, roomQuery);
        return roomList.getRecords().size() > 0 ? ResponseResult.SUCCESS(roomList) : ResponseResult.FAILED("无数据");
    }

    /**
     * 添加房间
     *
     * @param roomDTO 房间信息
     * @return 添加房间成功或者失败json数据
     */
    @ApiOperation("添加房间")
    @LogAnnotation(module = "addRoom", operator = "添加房间")
    @PostMapping("/adjuration")
    public ResponseResult addRoom(@RequestBody RoomDTO roomDTO) {
        String tips = roomService.addRoom(roomDTO);
        return "添加房间信息成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }


    /**
     * 编辑房间
     *
     * @param roomId  房间id
     * @param roomDTO 房间信息
     * @return 编辑成功或者编辑失败json数据
     */
    @ApiOperation("编辑房间")
    @LogAnnotation(module = "editRoom", operator = "编辑房间")
    @PutMapping("/update/{roomId}")
    public ResponseResult editRoom(@PathVariable String roomId, @RequestBody RoomDTO roomDTO) {
        String tips = roomService.editRoom(roomId, roomDTO);
        return "更新房间信息成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 批量添加房间信息
     *
     * @param roomDTO 房间信息
     * @return 返回批量添加成功或者失败json数据
     */
    @ApiOperation("批量添加房间信息")
    @LogAnnotation(module = "batchAddRoom", operator = "批量添加房间信息")
    @PostMapping("/batch_add")
    public ResponseResult batchAddRoom(@RequestBody RoomDTO roomDTO) {
        String tips = roomService.batchAddRoom(roomDTO);
        return "批量添加成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 删除/批量删除房间
     *
     * @param roomIds 房间id数组
     * @return 删除成功或者删除失败json数据
     */
    @ApiOperation("删除/批量删除房间")
    @LogAnnotation(module = "batchDelRoom", operator = "删除/批量删除房间")
    @DeleteMapping("/delete")
    public ResponseResult batchDelRoom(@RequestBody String[] roomIds) {
        if (roomIds.length == 0) {
            return ResponseResult.FAILED("房间id不能为空");
        }
        Integer result = roomService.batchDelRoom(roomIds);
        return result > 0 ? ResponseResult.SUCCESS("共删除：" + result + "条") : ResponseResult.FAILED("删除失败");
    }

    /**
     * 导出楼栋列表
     *
     * @param roomQuery 查询条件
     * @param response  响应
     */
    @ApiOperation("导出房间列表")
    @LogAnnotation(module = "exportRoomList", operator = "导出房间列表")
    @PostMapping("/export_room_list/{page}/{pageSize}")
    public void exportRoomList(@PathVariable int page, @PathVariable int pageSize,
                               @RequestBody(required = false) RoomQuery roomQuery, HttpServletResponse response) {
        if (page <= 0 || pageSize <= 0) {
            throw new RuntimeException();
        }
        IPage<RoomDTO> roomList = roomService.getRoomList(page, pageSize, roomQuery);
        EasyExcelExportUtils.exportToResponse(roomList.getRecords(), LocalDateTime.now().getDayOfMonth()+"_roomList.xlsx", "房间列表", RoomDTO.class, response, true);
    }
}
